_ = require 'underscore'
validator = require 'js-validator-ext'
validKeyWords = ["accepted", "after", "alpha", "alpha_dash", "alpha_num", "array"
, "before", "between", "boolean", "date", "date_format", "different", "digits"
, "digits_between", "email", "in", "integer", "ip", "max", "mimes", "min", "not_in"
, "numeric", "regex", "required", "required_if", "required_with", "required_with_all"
, "required_without", "required_without_all", "same", "size", "string", "url"]

ValidUtils =

  # 获取对象的值
  getObjVal: (params) ->
    tmpObj = {}

    keyCount = 0
    for k, v of params
      keyCount++

    if 1 is keyCount
      for k, v of params
        if not v
          tmpObj.hasVal = false
          params[k] = "{}"
        else
          tmpObj.hasVal = true

    tmpObj.params = params
    tmpObj.keyCount = keyCount
    return tmpObj

  # 校验对象
  validate: (params, rules, errorTips, isObjWrap) ->
    # 校验是否传入参数对象
    if not params or _.isEmpty params
      return {
        "status": "failed"
        "error": '参数对象不能为空！'
      }

    # 校验传入的参数对象是否有值
    paramsObj = ValidUtils.getObjVal params
    if 1 is paramsObj.keyCount
      if not paramsObj?.hasVal
        paramsStr = JSON.stringify params
        return {
          "status": "failed"
          "error": "参数对象不能为空！#{paramsStr}"
        }

    # 校验是否传入规则对象
    if not rules or _.isEmpty rules
      return {
        "status": "failed"
        "error": '验证规则不能为空！'
      }

    # 校验传入的规则对象是否有值
    rulesObj = ValidUtils.getObjVal rules
    if 1 is rulesObj.keyCount
      if not rulesObj?.hasVal
        rulesStr = JSON.stringify rules
        return {
          "status": "failed"
          "error": "验证规则不能为空！#{rulesStr}"
        }

    # 校验是否传入错误提示对象
    if not errorTips or _.isEmpty errorTips
      return {
        "status": "failed"
        "error": '错误提示对象不能为空！'
      }

    # 校验传入的错误提示对象是否有值
    errorTipsObj = ValidUtils.getObjVal errorTips
    if 1 is errorTipsObj.keyCount
      if not errorTipsObj?.hasVal
        errorTipsStr = JSON.stringify errorTips
        return {
          "status": "failed"
          "error": "验证规则不能为空！#{errorTipsStr}"
        }

    # 设置当某条验证失败时继续其他规则的验证
    validator.setConfig {resumeOnFailed: false}
    # 校验数据
    validResult = validator.validate params, rules

    if 'success' isnt '' + validResult?.status
      validResult['error'] = '参数校验不通过！'
      ValidUtils.setErrorTips validResult?.rejects, errorTips, errorTipsObj.keyCount, isObjWrap
    return validResult

  # 获取错误提示对象
  getErrorTipObj: (errorTips) ->
    tmpObj = {}
    for k, v of errorTips
      tmpObj = v
    return tmpObj

  # 设置错误提示
  setErrorTips: (rejects, errorTips, etsKeyCount, isObjWrap) ->
    if 0 isnt rejects?.length
      if 1 is etsKeyCount
        if not isObjWrap
          errorTipsObj = errorTips
        else
          errorTipsObj = ValidUtils.getErrorTipObj errorTips
        for rej in rejects
          rej['error'] = errorTipsObj?[rej?.field]?[rej?.rule]
      else
        for rej in rejects
          for etsKey, etsRules of errorTips
            if etsKey is rej?.field
              for ruleKey, ruleVal of etsRules
                if rej?.rule is ruleKey
                  rej['error'] = ruleVal
    return rejects

module.exports = ValidUtils